﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class XemNCCForm : Form
    {
        private string _maNCC;

        public XemNCCForm(string maNhaCungCap)
        {
            InitializeComponent();
            _maNCC = maNhaCungCap;
        }

        private void XemNCC_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.NhaCungCap s = dc.NhaCungCaps.Single(x => x.MaNhaCungCap == _maNCC);

            // Fill textboxs
            txtMaNCC.Text = s.MaNhaCungCap;
            txtName.Text = s.TenNhaCungCap;
            txtTaxCode.Text = s.MaThue;
            txtAddress.Text = s.DiaChi;
            txtFaxNumber.Text = s.SoFax;
            txtEmail.Text = s.Email;
            txtNote.Text = s.GhiChu;

            txtTradename.Text = s.TenGiaoDich;
            txtBankAccount.Text = s.TaiKhoanNganHang;
            txtPhoneNumber.Text = s.DienThoai;
            txtContractor.Text = s.NguoiDaiDien;
            txtStatus.Text = s.TrangThai;

            // Fill grid
            dgvItem.Rows.Clear();
            int count = 1;

            var items = from i in s.HangHoa_NhaCungCaps
                        where i.Dat == true &&
                              i.PhieuDanhGiaNCC.deleted == false &&
                              i.PhieuDanhGiaNCC.DaDuyet == true
                        group i by i.HangHoa into g
                        let newest = g.Max(i => i.PhieuDanhGiaNCC.NgayTao)
                        select g.Where(i => i.PhieuDanhGiaNCC.NgayTao == newest).Single();
            
            foreach (var v in items)
            {
                _DB.HangHoa_NhaCungCap i = v;

                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvItem);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = i.HangHoa.TenHangHoa;
                r.Cells[2].Value = i.ChatLuong;
                r.Cells[3].Value = i.Gia;
                r.Cells[4].Value = i.ThoiGianGiaoHang;
                r.Cells[5].Value = i.HauMai;
                r.Cells[6].Value = i.HinhThucThanhToan;
                r.Cells[7].Value = i.GhiChu;

                dgvItem.Rows.Add(r);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
